<?php
/**
 * Created by PhpStorm.
 * User: sks
 * Date: 2015/12/15
 * Time: 13:49
 */

class Involved_UserMessageModel extends BaseModel{

    protected $table = 'user_message';

    /**
     *获取信息列表
     * @return mixed
     */
    public function UserMessageList()
    {
        return  DB::table($this->table)
            ->orderBy('add_time','desc')
            ->get();
    }
    /**
     *根据ID获取信息列表
     * @return mixed
     */
    public function Ownmesg($user_id)
    {
        return  DB::table($this->table)
            ->where('user_id',$user_id)
            ->where('is_valid','1')
            ->orderBy('add_time','desc')
            ->paginate(10);
    }

    /**
     *根据ID获取信息列表
     * @return mixed
     */
    public function Ownmesgcount($user_id)
    {
        return  DB::table($this->table)
            ->where('user_id',$user_id)
            ->where('is_valid',1)
            ->where('is_read',0)
            ->count();
    }

    /**
     *根据ID获取信息列表
     * @return mixed
     */
    public function Owncount($user_id)
    {
        return  DB::table($this->table)
            ->where('user_id',$user_id)
            ->count();
    }


    /**
     *根据ID删除选中的
     * @return mixed
     */
    public function UserMessageDelect($user_id,$delitems)
    {
        return  DB::table($this->table)
            ->where('user_id',$user_id)
            ->whereIn('auto_id',$delitems)
            ->delete();
    }
    /**
     *根据ID修改状态
     * @return mixed
     */
    public function UserAccountsBiao($user_id,$auto_id)
    {
        return  DB::table($this->table)
            ->where('user_id',$user_id)
            ->where('auto_id',$auto_id)
            ->where('is_read',0)
            ->first();
    }

    /**
     *根据ID查询有没有
     * @return mixed
     */
    public function UserAccountsFirst($user_id,$delitems)
    {
        return  DB::table($this->table)
            ->where('user_id',$user_id)
            ->whereIn('auto_id',$delitems)
            ->where('is_read',0)
            ->first();
    }
    /**
     *根据ID查询有没有
     * @return mixed
     */
    public function UserAccountsFirstes($user_id,$delitems)
    {
        return  DB::table($this->table)
            ->where('user_id',$user_id)
            ->where('auto_id',$delitems)
            ->where('is_read',0)
            ->first();
    }

    /**
     *API获取我的消息列表
     * @return mixed
     */

    public function ApiOwnmesg($user_id,$page)
    {
        $offset = ($page-1) * HostEnum::PAGE_NUM;
        return DB::table($this->table)
            ->select('auto_id','title','content','is_valid','add_time','user_id','is_read')
            ->where('user_id',$user_id)
            ->where('is_valid',1)
            ->skip($offset)->take(HostEnum::PAGE_NUM)
            ->orderBy('add_time','desc')
            ->get();
    }
    /**
     *API获取我的未读消息数量
     * @return mixed
     */
    public function  MessageCount($user_id){
        return  DB::table($this->table)
            ->where('user_id',$user_id)
            ->where('is_valid',1)
            ->where('is_read',0)
            ->count();

    }

    /**
     *web获取我的消息详情
     * @return mixed
     */

    public function WebOwnmesgCont($user_id,$auto_id)
    {

        return DB::table($this->table)
            ->select('auto_id','title','content','is_valid','add_time','user_id','is_read')
            ->where('user_id',$user_id)
            ->where('is_valid',1)
            ->where('auto_id',$auto_id)
            ->orderBy('add_time','desc')
            ->first();
    }




}